﻿// =================================================================== 
// 项目说明
//====================================================================
// liucx @Copy Right 2019
// 文件： GroupCommentRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/10/12
// 负责人：liucx
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using Dapper;
using YouWei.Infrastructure.Models;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///群空间内容 仓储层
    /// </summary>	
    public class GroupCommentRepository : BaseRepository<GroupComment>
    {

        #region 构造

        public GroupCommentRepository() : base() { }

        public GroupCommentRepository(string connectionString) : base(connectionString) { }

        public GroupCommentRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 查询群组信息列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<GroupComment> Pages(int pageIndex, int pageSize, GroupCommentSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT * FROM [GroupComment] T1 WITH(NOLOCK) WHERE T1.IsActvie=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }
            sb.Append(" ORDER BY T1.GroupCommentID DESC");
            return Pages(pageIndex, pageSize, sb.ToString(), dparams);
        }
        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(GroupCommentSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (model.UserID > 0)
            {
                sb.Append(" AND T1.UserID=@UserID");
                dparams.Add("UserID", model.UserID, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrWhiteSpace(model.OpenID))
            {
                sb.Append(" AND T1.OpenID=@OpenID");
                dparams.Add("OpenID", model.OpenID, System.Data.DbType.String);
            }
            else if (model.GroupCommentID > 0)
            {
                sb.Append(" AND T1.GroupCommentID=@GroupCommentID");
                dparams.Add("GroupCommentID", model.GroupCommentID, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrEmpty(model.CommentName))
            {
                sb.Append(" AND charindex(@CommentName,T1.CommentName)>0");
                dparams.Add("CommentName", model.CommentName, System.Data.DbType.String);
            }
            if (!string.IsNullOrEmpty(model.CommentDesc))
            {
                sb.Append(" AND charindex(@CommentDesc,T1.CommentDesc)>0");
                dparams.Add("CommentDesc", model.CommentDesc, System.Data.DbType.String);
            }

        }
        /// <summary>
        /// 根据ID获取详细信息，包含detail
        /// </summary>
        /// <param name="GroupCommentID"></param>
        /// <returns></returns>
        public GroupCommentDto GetGroupCommentInfoById(int GroupCommentID)
        {
            //StringBuilder sb = new StringBuilder(@"select t1.*,t2.Detail,t3.NickName as WxNickName, t3.HeadImgUrl as WxHeadImgUrl 
            //from GroupComment t1 with(nolock) left join GroupCommentDetail t2 on t1.GroupCommentID=t2.GroupCommentID ");
            //sb.Append("Left JOIN WeChartUser t3 WITH(NOLOCK) ON t1.OpenID=t3.OpenID");
            //sb.Append(" where t1.GroupCommentID=@GroupCommentID");
            string sql = @" SELECT  t1.*,
                                    t2.Detail,
                                    t3.UserName AS WxNickName,
                                    t3.UserHead AS WxHeadImgUrl
                            FROM    GroupComment t1 WITH ( NOLOCK )
                                    LEFT JOIN GroupCommentDetail t2  ON t1.GroupCommentID = t2.GroupCommentID
                                    LEFT JOIN UserInfo t3 WITH ( NOLOCK )   ON t1.UserID = t3.UserID
                            WHERE   t1.GroupCommentID = @GroupCommentID";
            return dbContext.Connection.QueryFirstOrDefault<GroupCommentDto>(sql, new { GroupCommentID = GroupCommentID });
        }

        public Page<GroupCommentDto> Pages(int pageIndex, int pageSize, string openID, int Section, int isHot, int carGroupId, string orderName, string orderAsc = "ASC")
        {
            //string sql = "SELECT t1.*,t2.NickName as WxNickName, t2.HeadImgUrl as WxHeadImgUrl FROM [GroupComment] t1 WITH(NOLOCK) Left JOIN WeChartUser t2 WITH(NOLOCK) ON t1.OpenID=t2.OpenID ";
            //sql += "  WHERE t1.IsActvie=1 AND t1.IsShow=1";
            string sql = @" SELECT  t1.*,
                                    t2.UserName AS WxNickName,
                                    t2.UserHead AS WxHeadImgUrl
                            FROM    [GroupComment] t1 WITH ( NOLOCK )
                                    LEFT JOIN UserInfo t2 WITH ( NOLOCK ) ON t1.UserID = t2.UserID
                            WHERE    t1.IsActvie = 1 AND  t1.IsShow = 1";
            //所属群组id
            if (carGroupId > 0)
            {
                sql += "AND t1.CarGroupID=" + carGroupId;
            }
            //热门推荐不区分板块类别
            if (isHot == 1)
            {
                sql += "AND t1.IsHot=1";
            }
            else
            {
                sql += "AND  t1.CommentSection=" + Section ;
            }
            sql += "   ORDER BY t1." + orderName + "  " + orderAsc;
            return dbContext.Connection.Page<GroupCommentDto>(pageIndex, pageSize, sql);
        }

        /// <summary>
        /// 添加点赞
        /// </summary>
        /// <param name="CommentId"></param>
        public void AddPraiseCount(int GroupCommentID)
        {
            Execute("UPDATE [GroupComment] SET PraiseCount=PraiseCount+1 WHERE GroupCommentID=@GroupCommentID", new { GroupCommentID = GroupCommentID });
        }

        /// <summary>
        /// 添加浏览
        /// </summary>
        /// <param name="CommentId"></param>
        public void AddPageViews(int GroupCommentID)
        {
            Execute("UPDATE [GroupComment] SET PageViews=PageViews+1 WHERE GroupCommentID=@GroupCommentID", new { GroupCommentID = GroupCommentID });
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="CommentId"></param>
        public void AddEvaluateCount(int GroupCommentID)
        {
            Execute("UPDATE [GroupComment] SET EvaluateCount=EvaluateCount+1 WHERE GroupCommentID=@GroupCommentID", new { GroupCommentID = GroupCommentID });
        }
    }
}